//
//  CommentTableViewController.m
//  FootBallTest_huang
//
//  Created by lanou3g on 15/6/1.
//  Copyright (c) 2015年 lanou3g.com. All rights reserved.
//

#import "CommentTableViewController.h"
#import "NewsDetailsTableViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "NewsDetailsModel.h"
#import "NewsDetailsCommentsModle.h"
#import "CommentOneModle.h"
#import "CommentTwoModle.h"
#import "CommentOneTableViewCell.h"
#import "CommentTwoTableViewCell.h"
#import "MJRefresh.h"
@interface CommentTableViewController ()
@property(nonatomic,assign)BOOL isParse;
@end

@implementation CommentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.isParse = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self stringWithFormat:[NSString stringWithFormat:@"http://games.mobileapi.hupu.com/1/6.5.2/csl/getNewsDetail?client=491606813622600&nid=%@",self.newsModle.nid]];

    
   
}



- (void)stringWithFormat:(NSString *)url
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    self.CommentTitleArray = [NSMutableArray array];
    self.CommentInfoArray = [NSMutableArray array];
   
    // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
    __weak CommentTableViewController * NDTVC = self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject == nil) {
            return ;
        }
        NSMutableDictionary * BigDic = responseObject;
        NSMutableDictionary * dic = [BigDic objectForKey:@"result"];
        
        CommentOneModle * commentOneModle = [[CommentOneModle alloc] init];
        [commentOneModle setValuesForKeysWithDictionary:dic];
        [NDTVC.CommentTitleArray addObject:commentOneModle];
        
        NSMutableArray * commentArray = [dic objectForKey:@"light_comments"];
       
        for (NSMutableDictionary * smallDic in commentArray) {
            CommentTwoModle * commentTwoModle = [[CommentTwoModle alloc] init];
            [commentTwoModle setValuesForKeysWithDictionary:smallDic];
            
            [NDTVC.CommentInfoArray addObject:commentTwoModle];
            NSLog(@"newsDetailsCommentsModle === %@",commentOneModle);
            
        }
        NDTVC.isParse =YES;
        [NDTVC.tableView reloadData];
      
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}



//切割
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.frame = CGRectMake(0, 0, self.view.frame.size.width,CGRectGetHeight(tableView.window.frame)-105);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    
      NSLog(@"_CommentInfoArray.count == %ld",_CommentInfoArray.count);
    if (self.isParse == YES) {
        return 1+_CommentInfoArray.count;
      
    }else{
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row == 0) {
        [self.tableView registerNib:[UINib nibWithNibName:@"CommentOneTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"oneCell"];
        CommentOneModle * oneModle = _CommentTitleArray[0];
        CommentOneTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"oneCell"];
        cell.title.text = oneModle.title;
        
        //时间(多少。。。之前)******
        
        NSString * returnDateString=nil;
        
        NSDate *confromTimesp =[NSDate dateWithTimeIntervalSince1970:[oneModle.addtime integerValue]];
                NSDate *nowdate = [NSDate date];
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                [formatter setDateFormat:@"YYYYMMddHHmmss"];
                NSString *nowdateString = [formatter stringFromDate:nowdate];
        
                NSString *addtime = [formatter stringFromDate:confromTimesp];
        
        NSTimeInterval nowDateInterval = [[NSDate date] timeIntervalSince1970];
        
        NSTimeInterval twoTimeInterval = nowDateInterval - [oneModle.addtime integerValue];
        
        
        NSInteger  nowDateInteger = [nowdateString integerValue];
        NSInteger  addTimeInteger = [addtime integerValue];
        NSInteger  twoTimeInterV = nowDateInteger - addTimeInteger;
        if (twoTimeInterV>2000000) {
            returnDateString = addtime;
//            NSLog(@"%@",addtime);
            
        }else if(twoTimeInterV>1000000&&twoTimeInterV<2000000){
            
//            NSLog(@"昨天");
            
        }else {
            
            if (twoTimeInterval>3600) {
                NSInteger fffff = (NSInteger)twoTimeInterval;
                NSInteger hour =fffff/3600;
                returnDateString = [NSString stringWithFormat:@"%ld 小时之前",(long)hour];
            }else{
             
                NSInteger minit =(NSInteger)twoTimeInterval%60;
                returnDateString = [NSString stringWithFormat:@"%ld 分钟之前",(long)minit];
            
            }
            
            
            
        }
        
        cell.addtime.text =returnDateString;
        
        //时间(多少。。。之前)******
        
//        NSLog(@"%@",cell.addtime.text);
        
        cell.origin.text = oneModle.origin;
        return cell;
    }else{
        [self.tableView registerNib:[UINib nibWithNibName:@"CommentTwoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"twoCell"];
        CommentTwoModle * towModle = _CommentInfoArray[indexPath.row-1];
        
        CommentTwoTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"twoCell"];
        cell.user_name.text = towModle.user_name;
        
        NSString * returnDateString=nil;
        
        NSDate *confromTimesp =[NSDate dateWithTimeIntervalSince1970:[towModle.create_time integerValue]];
        NSDate *nowdate = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"YYYYMMddHHmmss"];
        NSString *nowdateString = [formatter stringFromDate:nowdate];
        
        NSString *addtime = [formatter stringFromDate:confromTimesp];
        
        NSTimeInterval nowDateInterval = [[NSDate date] timeIntervalSince1970];
        
        NSTimeInterval twoTimeInterval = nowDateInterval - [towModle.create_time integerValue];
        
        
        NSInteger  nowDateInteger = [nowdateString integerValue];
        NSInteger  addTimeInteger = [addtime integerValue];
        NSInteger  twoTimeInterV = nowDateInteger - addTimeInteger;
        if (twoTimeInterV>2000000) {
            returnDateString = addtime;
            
            
        }else if(twoTimeInterV>1000000&&twoTimeInterV<2000000){
            
          
            
        }else {
            
            if (twoTimeInterval>3600) {
                NSInteger fffff = (NSInteger)twoTimeInterval;
                NSInteger hour =fffff/3600;
                returnDateString = [NSString stringWithFormat:@"%ld 小时之前",hour];
            }else{
                
                NSInteger minit =(NSInteger)twoTimeInterval%60;
                returnDateString = [NSString stringWithFormat:@"%ld 分钟之前",minit];
                
            }
            
            
            
        }
        
        cell.create_time.text =returnDateString;
        
        cell.content.text = towModle.content;
        cell.light_count.text = towModle.light_count;
        return cell;
        
    }
    
    
}



#pragma mark---计算文本高度
- (CGFloat)textLabelHeight:(NSString *)text
{
    
    CGSize size = CGSizeMake(self.view.frame.size.width, 10000000);
    NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16.0]};
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.height;
}


#pragma mark---设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        CommentOneModle * oneModle = _CommentTitleArray[0];
        CGFloat heingt = [self textLabelHeight:oneModle.title];
        if (heingt+21 < 75) {
            return 75;
        }
        return heingt+21;
    }else{
        CommentTwoModle * twoModle = _CommentInfoArray[indexPath.row-1];
        CGFloat height = [self textLabelHeight:twoModle.content];
        if (height+70<100) {
            return 100;
        }
        return height+70;
    }
    
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
